package com.dragon.repository.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.repository.config.annotation.DataScope;
import com.dragon.repository.entity.PlanCollect;
import com.dragon.repository.entity.Produce;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 生产表 Mapper 接口
 * </p>
 *
 * @author sy
 * @since 2023-06-25
 */
@Mapper
public interface ProduceMapper extends BaseMapper<Produce> {


    @Select("<script>" +
            "SELECT M.* FROM" +
            "(SELECT a.*, b.department_id as contract_department_id, b.order_time as orderTime,  " +
            " b.name AS contractName,c.username AS applicantName ,cc.username as responsibleName, dc.customer_name as customerName " +
            "FROM dragon_produce a " +
            "JOIN dragon_contract b ON a.contract_id = b.id and b.deleted=0 and b.status = 1 " +
            "LEFT JOIN dragon_customer dc ON b.customer_id = dc.id " +
            "LEFT JOIN dragon_admin c ON a.admin_id = c.id  " +
            "left join dragon_admin cc on a.responsible_id = cc.id " +
            "  ) M   " +
            "${ew.customSqlSegment} " +
            "</script>")
    @DataScope(adminAlias = "M", adminField = "responsible_id,admin_id", deptAlias = "M", deptField = "department_id,contract_department_id", authCodeString = "produce")
    Page<Produce> selectProduce(Page<Produce> producePage, @Param(Constants.WRAPPER) QueryWrapper<Produce> wrapper);

    @Select("SELECT M.* FROM(SELECT a.*,b.name AS contractName,c.username AS username,cc.username as responsibleName FROM dragon_produce a " +
            "LEFT JOIN dragon_contract b ON a.contract_id = b.id " +
            "LEFT JOIN dragon_admin c ON a.admin_id = c.id  " +
            "left join dragon_admin cc on a.responsible_id = cc.id "+
            ") M where 1=1 and M.contract_id = #{contractId} and M.status = 1 " )
    List<Produce> selectProduceByContractId(Integer contractId);

    @Select("SELECT M.* FROM(SELECT a.*,b.name AS contractName,c.username AS applicationName,cc.username as responsibleName FROM dragon_produce a " +
            "LEFT JOIN dragon_contract b ON a.contract_id = b.id " +
            "LEFT JOIN dragon_admin c ON a.admin_id = c.id  " +
            "left join dragon_admin cc on a.responsible_id = cc.id "+
            ") M where 1=1 and M.contract_id = #{contractId} and M.status = 1 " )
    Produce selectDetailByContractId(Integer contractId);

    @Select("SELECT M.* FROM(SELECT a.*,b.name AS contractName,c.username AS username,cc.username as responsibleName  FROM dragon_produce a " +
            "LEFT JOIN dragon_contract b ON a.contract_id = b.id " +
            "LEFT JOIN dragon_admin c ON a.admin_id = c.id  " +
            "left join dragon_admin cc on a.responsible_id = cc.id "+
            ") M WHERE 1 = 1 AND M.id = #{id} "
            )
   Produce selectProduceById(Integer id);

    @Select("<script>" +
            "SELECT a.*  FROM `dragon_produce` a where 1=1 " +
            "<if test='time != null and time != \"\"'>" +
            " and DATE_FORMAT(a.create_time,'%Y-%c') = #{time} or DATE_FORMAT(a.create_time,'%Y-%m') = #{time} " +
            "</if>" +
            "<if test='departmentIds != null and departmentIds.size!=0'>" +
            "AND a.department_id in" +
            "<foreach collection = 'departmentIds' item = 'item' open = '(' close = ')' separator = ','>" +
            "#{item}" +
            "</foreach>" +
            "</if>" +
            "<if test='adminId != null and adminId != \"\"'>" +
            "AND a.admin_id = #{adminId} "+
            "</if>" +
            "</script>")
    List<Produce> selectListByTime(String time,List<String> departmentIds,Integer adminId);

    @Select("SELECT MAX(number) FROM dragon_produce WHERE number LIKE CONCAT(#{code},'%')")
    String maxRoundInoutNumber(String code);

    @Select("SELECT M.* FROM (" +
            "SELECT a.id, a.contract_id,b.address as contractAddress,b.customer_id as customerId from dragon_produce a " +
            "LEFT JOIN dragon_contract b on a.contract_id = b.id ) M WHERE 1=1 AND M.id = #{id} ")
    Produce selectAddress(Integer produceId);

    /**
     * 生产 年数量
     * @return
     */
    @Select("SELECT count(*) FROM dragon_produce dp " +
            "left join dragon_contract dc on dp.contract_id = dc.id " +
            "where dp.status = 1 and dc.status = 1 and dc.deleted = 0  ")
    @DataScope(adminAlias = "dp",adminField = "admin_id",deptAlias = "dp",deptField = "department_id", authCodeString = "produce")
    Long selectYearProduceCount();

    /**
     * 生产 月数量
     * @return
     */
    @Select("SELECT count(*) FROM dragon_produce dp " +
            "left join dragon_contract dc on dp.contract_id = dc.id " +
            "where dp.status = 1 and dc.status = 1 and dc.deleted = 0 and left(dc.order_time, 7) = left(Now() ,7) ")
    @DataScope(adminAlias = "dp",adminField = "admin_id",deptAlias = "dp",deptField = "department_id",  authCodeString = "produce")
    Long selectMonthProduceCount();

    @Select("SELECT M.* FROM (SELECT dp.*, dc.name as contractName,dc.num contractCode,dc.order_time orderTime,dc2.customer_name customerName " +
            " from dragon_produce dp  " +
            "LEFT JOIN dragon_contract dc on dp.contract_id = dc.id " +
            "LEFT JOIN dragon_customer dc2 on dc.customer_id = dc2.id )M " +
            "${ew.customSqlSegment}")
    Page<Produce> selectNotDeliver(Page<Produce> page, @Param(Constants.WRAPPER) QueryWrapper<Produce> wrapper);
}
